package com.wx.tedu;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ChangedTxt {

    protected String existsDirectory() {
        String directoryPath;
        File directory;
        Scanner scan = new Scanner(System.in);
        do {
            System.out.print("输入文件所在文件夹的路径,名称(例:E:\\Txt),然后回车键:");
            directoryPath = scan.next();
            directory = new File(directoryPath);
            if (!(directory.exists() && directory.isDirectory())) {
                System.out.println(directoryPath + "文件夹路径不存在或者不是文件夹！输入正确的文件夹路径！");
            }
        } while (!(directory.exists() && directory.isDirectory()));
        return existsTxt(directory);
    }

    protected String existsOutDirectory() {
        String directoryOutPath = "C:\\Txt\\Out";
        File outDirectory = new File(directoryOutPath);

        if (outDirectory.exists() && outDirectory.isDirectory()) {
            System.out.println("程序输出目录：" + directoryOutPath);
        } else {
            System.out.println("开始创建程序输出目录！");
            if (outDirectory.mkdirs()) {
                System.out.println("程序输出目录创建成功！程序输出目录：" + directoryOutPath);
            } else {
                System.out.println("程序输出目录创建成功！此处截图！");
            }
        }
        return outDirectory.getPath();
    }

    protected String existsTempDirectory() {
        String directoryTempPath = "C:\\Txt\\Temp";
        File tempDirectory = new File(directoryTempPath);

        if (tempDirectory.exists() && tempDirectory.isDirectory()) {
            System.out.println("程序临时目录：" + directoryTempPath);
        } else {
            System.out.println("开始创建程序临时目录！");
                if (tempDirectory.mkdirs()) {
                    System.out.println("程序临时目录创建成功！程序临时目录：" + directoryTempPath);
                } else {
                    System.out.println("程序临时目录创建成功！此处截图！");
                }
        } return tempDirectory.getPath();
    }

    protected String existsTxt(File directory) {
        String fileName;
        File file;
        Scanner scan = new Scanner(System.in);
        do {
            System.out.print("请输入当前" + directory.getName() + "文件夹下需要操作的文件名全称(例:操作文件.txt),然后回车键:");
            fileName = scan.next();
            file = new File(directory.getPath() + "\\" + fileName);
            if (!file.exists()) {
                System.out.println(directory.getName() + "文件夹下" + fileName + "文件不存在！输入正确的文件名全称！");
            }
        } while (!file.exists());
        return file.getPath();
    }

    protected String changedTxt() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());

        File srcFile = new File(existsDirectory());
        File outFile = new File(existsTempDirectory() + "\\" + (formatter.format(date)) + "_temp.txt");

        try (InputStreamReader isr = new InputStreamReader(Files.newInputStream(srcFile.toPath()), "GB2312"); //ANSI编码
             OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(outFile.toPath()), StandardCharsets.UTF_8); //存为UTF-8
        ) {
            System.out.println("\n程序开始文档转码UTF-8\n");

            int len = isr.read();
            while (-1 != len) {
                osw.write(len);
                len = isr.read();
            }
            osw.flush();
            isr.close();
            osw.close();
            System.out.println("程序执行文档转码UTF-8完成！");
            System.out.println("文档转码后文件所在位置：" + outFile.getPath()+"\n");
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage() + "此处截图！");
        }
        return outFile.getPath();
    }
}


